// pages/search/search.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    currentIndex: 0,
    recommendBoolean: true,
    nohistory: false,
    historys: [],
    tabs: ['全部', '院校', '专业'],
    schoolList: [],
    minSchoolList: [],
    majorList: [],
    minMajorList: [],
    searchBealoon: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.searchSchool("");
    // this.searchMajor("");
    // this.search()
    let historys = wx.getStorageSync('historys')
    this.setData({
      historys: historys
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    // this.onRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navToInfo(e) {
    wx.navigateTo({
      url: '/pages/search/moreInfo/moreInfo',
    })
  },
  navToSchool(e) {
    wx.navigateTo({
      url: '/pages/search/moreSchool/moreSchool',
    })
  },
  clickTab(e) {
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    })
  },
  clickBtn(e) {
    this.setData({
      currentIndex: e.currentTarget.dataset.btnindex
    })
  },
  //刷新
  onRefresh() {
    let currentIndex = this.data.currentIndex;
    // console.log(currentIndex);
    if (currentIndex == 3) {
      console.log('log', 3);
    }
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.getData();
  },
  //网络请求，获取数据
  getData() {
    wx.request({
      url: '',
      //网络请求执行完后将执行的动作
      complete(res) {
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      }
    })
  },
  //搜索院校
  searchSchool(schoolName) {
    let that = this;
    app.globalData.wxRequest.requestGet('school/getList.do', schoolName).then(res => {
      // console.error('res',res);
      that.setData({
        schoolList: res.data.result,
        minSchoolList: res.data.result.slice(0, 4)
      })
    }).catch(res => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    });
  },
  //搜索专业
  searchMajor(majorName) {
    let that = this;
    app.globalData.wxRequest.requestGet('major/searchMajor.do', majorName).then(res => {
      // console.error('res',res);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      that.setData({
        majorList: res.data.result,
        minMajorList: res.data.result.slice(0, 4)
      })
    }).catch(res => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    });
  },
  search(e){
    let that = this;
    let val = e.detail.searchVal;
    console.log('val',val);
    that.search_simple(val,that)
  },
  clickhistory(e){
    let that = this;
    let val = e.currentTarget.dataset.txt;
    that.search_simple(val,that)
  },
  search_simple(searchVal,that) {
    let historys = wx.getStorageSync('historys');
    console.log(historys);
    if (!historys) {
      historys = []
    }
    app.globalData.wxRequest.requestGet('data/search.do', {
      key: searchVal
    }).then(res => {
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      console.log(res);
      if (!historys.length) {
        historys[0] = searchVal
      } else {
        historys.unshift(searchVal)
        historys.slice(-1,7)
      }
      // 历史记录去重
      historys = that.unique(historys)
      wx.setStorageSync('historys', historys)

      that.setData({
        schoolList: res.data.school,
        minSchoolList: res.data.school.slice(0, 4),   
        majorList: res.data.major,
        minMajorList: res.data.major.slice(0, 4),     
        searchBealoon: false,
      })
    }).catch(err => {
      wx.showToast({
        title: '请求数据获取',
        icon: 'error'
      })
    })
  },
  // 数组去重
  unique(arr) {
    return Array.from(new Set(arr))
  },
  tomajor(e){
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/majorinfo/majorinfo?id=' + id,
    })
  },
  toSchoolInfo(e) {
    let id = e.currentTarget.dataset.schoolid;
    wx.navigateTo({
      url: '/pages/schoolinfo/schoolinfo?id=' + id,
    })
  },
})